layui.use(['admin', 'ax', 'table' ,'element' ,'form'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var admin = layui.admin;
    var table = layui.table;
    var form = layui.form;

    var Current = {
        tableId: "table",
        data: [] ,
        checkedData: []
    };

    Current.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, sort: true, title: 'id'},
            {field: 'sign', sort: true, title: '标签'},
            {field: 'title', sort: true, title: '标题'},
            {field: 'content', sort: true, title: '内容' },
            {field: 'createdTime', sort: true, title: '创建时间' },
            {field: 'updatedTime', sort: true, title: '更新时间' },
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    Current.onSearch = function () {
        var queryData = {
            sign : $("#sign").val()
        };
        table.reload(Current.tableId, {where:queryData});
    };


    Current.onAdd = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '添加模板',
            area: ['60%','70%'],
            content: system.contextPath + '/common/dataTemplate/toAdd',
            end: function () {
                admin.getTempData('formOk') && table.reload(Current.tableId);
            }
        });
    };

    Current.onDelete = function (data) {
        var operation = function () {
            var ajax = new $ax(system.contextPath + '/common/dataTemplate/delete/id/' + data.id, function (res) {
                if (res.status) {
                    system.success("删除成功!");
                    table.reload(Current.tableId);
                } else {
                    system.error("删除失败!" + res.msg + "!");
                }
            }, function (res) {
                system.error("删除失败!" + res.msg + "!");
            });
            ajax.start();
        };
        system.confirm("是否删除?", operation);
    };

    Current.onEdit = function (data) {
        admin.putTempData('formOk', false);
        top.layui.admin.open({
            type: 2,
            title: '编辑',
            area: ['60%', '70%'],
            content: system.contextPath + '/common/dataTemplate/toEdit?id=' + data.id,
            end: function () {
                admin.getTempData('formOk') && table.reload(Current.tableId);
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + Current.tableId,
        url: system.contextPath + '/common/dataTemplate/search/page',
        page: true,
        request: {
            pageName: 'current' //页码的参数名称，默认：page
            , limitName: 'size' //每页数据量的参数名，默认：limit
        },
        height: "full-158",
        cellMinWidth: 100,
        cols: Current.initColumn()  ,parseData: function(res){ //res 即为原始返回的数据
            return {
                "code": res.code, //解析接口状态
                "msg": res.msg, //解析提示文本
                "count": res.data.total, //解析数据长度
                "data": res.data.records //解析数据列表
            };
        },
        response: {
            statusCode: 200
        }
        ,size: 'lg',
        done: function (res, curr, count) {
            count || this.elem.next('.layui-table-view').find('.layui-table-header').css('overflow', 'visible');
            layui.each($('select'), function (index, item) {
                var elem = $(item);
                elem.val(elem.data('value')).parents('div.layui-table-cell').css('overflow', 'visible');
            });
            form.render();
        }
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        Current.onAdd();
    });

    //搜索按钮点击事件
    $("#btnSearch").click(function(){
        Current.onSearch();
    });

    // 工具条点击事件
    table.on('tool(' + Current.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'delete') {
            Current.onDelete(data);
        }else if(layEvent === 'edit'){
            Current.onEdit(data);
        }
    });
});
